package net.oschina.team.note.controller;

import java.util.List;

import net.oschina.team.note.bean.Notes;
import net.oschina.team.note.bean.NotesTemplate;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Model;
import com.mysql.jdbc.StringUtils;

@SuppressWarnings("unused")
public class NotesController extends Controller {
	//获取所有符合条件的即时贴信息
	public void getAllNotes(){
		int user = getParaToInt("user");
		int status = getParaToInt("status", Notes.STATUS_NORMAL);
		if(NotesTemplate.getByUser(user) == null){
			if(NotesTemplate.initByUser(user)){
				Notes.ME.createAsTemplate(user, NotesTemplate.DEFAULT_POSITION_X, NotesTemplate.DEFAULT_POSITION_Y, NotesTemplate.DEFAULT_POSITION_Z);
			}
		}
		String sql = "SELECT * FROM notes WHERE user = ? AND status = ?";
		List<Notes> notesList = Notes.ME.find(sql, user, status);
		renderJson(notesList);
	}
	//更新即时贴长宽和即时贴头部距离左侧距离
	public void updateSize(){
		int id = getParaToInt("id");
		int width = getParaToInt("width");
		int height = getParaToInt("height");
		long title_left = getParaToLong("title_left");
		Notes.ME.set("id", id).set("width", width).set("height", height).set("title_left", title_left).update();
		renderNull();
	}
	//更新即时贴的z-index值
	public void updatePositionZ(){
		int id = getParaToInt("id");
		int positionZ = getParaToInt("positionZ");
		Notes.ME.set("id", id).set("position_z", positionZ).update();
		renderNull();
	}
	//更新即时贴的位置
	public void updatePosition(){
		int id = getParaToInt("id");
		int position_x = getParaToInt("position_x");
		int position_y = getParaToInt("position_y");
		Notes.ME.set("id", id).set("position_x", position_x).set("position_y", position_y).update();
		renderNull();
	}
	//更新即时贴内容
	public void updateContent(){
		int id = getParaToInt("id");
		String content = getPara("content");
		Notes.ME.set("id", id).set("content", content).update();
		renderNull();
	}
	//更新即时贴颜色
	public void updateColor(){
		String color = getPara("color", "yellow");
		Notes.ME.set("color", color).update();
		render(color);
	}
	//创建新的即时贴
	public void createNew(){
		int userId = getParaToInt("userId");
		int x = getParaToInt("x");
		int y = getParaToInt("y");
		int z = getParaToInt("z");
		Notes notes = Notes.ME.createAsTemplate(userId,x,y,z);
		if(notes != null){
			renderJson(notes);
		}else{
			renderNull();
		}
	}
	//删除即时贴
	public void deleteNote(){
		int noteId = getParaToInt("noteId");
		boolean result = Notes.ME.deleteById(noteId);
		renderNull();
	}
}
